package cn.com.utt.unms.controller;

import java.security.Principal;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;

import cn.com.utt.common.util.LocaleMessageSourceUtil;
import cn.com.utt.common.util.Tips;
import cn.com.utt.common.vo.RestResult;
import cn.com.utt.common.vo.User;
import cn.com.utt.unms.pojo.Group;
import cn.com.utt.unms.service.GroupService;

@RestController
@RequestMapping("v1/groups")
public class GroupController {
	
	@Autowired
    private LocaleMessageSourceUtil messageSourceUtil;
	
	@Autowired
	private GroupService groupService;
	
	@RequestMapping(value = "/save", method = { RequestMethod.POST }, consumes = "application/json")
	public Map<String, Object> save(@RequestBody Group group,Principal principal) {
		
		RestResult result = new RestResult();
		
		Authentication authentication = (Authentication) principal;
		User user = (User)authentication.getPrincipal();
		
		if(group.getpId() == null){
			result.setErrorCode(1);
			result.setErrorMsg(messageSourceUtil.getMessage(Tips.InvalidParameter));
			return result.toJsonObject();
		}
		
		if(group.getName().equals("")){
			result.setErrorCode(1);
			result.setErrorMsg(messageSourceUtil.getMessage(Tips.NoGroupName));
			return result.toJsonObject();
		}
		if(group.getName().length() > 64){
			result.setErrorCode(1);
			result.setErrorMsg(messageSourceUtil.getMessage(Tips.GroupNameNotGreaterThan64Letters));
			return result.toJsonObject();
		}
		if(!groupService.hasRight(user.getGroupId(),group.getpId())){
			result.setErrorMsg(messageSourceUtil.getMessage(Tips.PermissionDenied));
			return result.toJsonObject();
		}
		System.out.println("group-save:    "+JSON.toJSONString(group));
		groupService.add(group);
		result.setErrorMsg(messageSourceUtil.getMessage(Tips.OperationSucceed));
		return result.toJsonObject();
	}
	
	@RequestMapping(value = "/update", method = { RequestMethod.POST }, consumes = "application/json")
	public Map<String, Object> update(@RequestBody Group group,Principal principal) {
		RestResult result = new RestResult();
		Authentication authentication = (Authentication) principal;
		User user = (User)authentication.getPrincipal();
		if(group.getId() == null){
			result.setErrorCode(1);
			result.setErrorMsg(messageSourceUtil.getMessage(Tips.InvalidParameter));
			return result.toJsonObject();
		}
		if(group.getName().equals("")){
			result.setErrorCode(1);
			result.setErrorMsg(messageSourceUtil.getMessage(Tips.NoGroupName));
			return result.toJsonObject();
		}
		if(group.getName().length() > 64){
			result.setErrorCode(1);
			result.setErrorMsg(messageSourceUtil.getMessage(Tips.GroupNameNotGreaterThan64Letters));
			return result.toJsonObject();
		}
		if(!groupService.hasRight(user.getGroupId(),group.getId())){
			result.setErrorCode(1);
			result.setErrorMsg(messageSourceUtil.getMessage(Tips.PermissionDenied));
			return result.toJsonObject();
		}
		System.out.println("group-update:    "+JSON.toJSONString(group));
		groupService.update(group);
		result.setErrorMsg(messageSourceUtil.getMessage(Tips.OperationSucceed));
		return result.toJsonObject();
	}
	
	
	@RequestMapping(value = "/delete", method = { RequestMethod.POST })
	@ResponseBody
	public Map<String, Object> delete(Long groupId,Principal principal) {
		Authentication authentication = (Authentication) principal;
		User user = (User)authentication.getPrincipal();
		RestResult result = new RestResult();
		if(groupId == null){
			result.setErrorCode(1);
			result.setErrorMsg(messageSourceUtil.getMessage(Tips.InvalidParameter));
			return result.toJsonObject();
		}
		if(user.getGroupId().equals(groupId)){
			result.setErrorCode(1);
			result.setErrorMsg(messageSourceUtil.getMessage(Tips.DoDeleteRoot));
			return result.toJsonObject();
		}
		if(!groupService.hasRight(user.getGroupId(),groupId)){
			result.setErrorMsg(messageSourceUtil.getMessage(Tips.PermissionDenied));
			return result.toJsonObject();
		}
		result = groupService.delete(groupId);
		return result.toJsonObject();
	}
	
	@RequestMapping(value = "/tree", method = { RequestMethod.GET })
	public Map<String, Object> tree(Principal principal,Boolean net,Long childrenId) {
		Authentication authentication = (Authentication) principal;
		User user = (User)authentication.getPrincipal();
		RestResult result = new RestResult();
		List<Group> tree = groupService.queryGroupsTree(user.getGroupId(),net,childrenId);
		result.put("zNodes", tree);
		return result.toJsonObject();
	}
	
}
